<?php
/**
    * 导出数据为excel表格
    *@param $data    一个二维数组,结构如同从数据库查出来的数组
    *@param $title   excel的第一行标题,一个数组,如果为空则没有标题
    *@param $filename 下载的文件名
    *@examlpe 
    $stu = M ('User');
    $arr = $stu -> select();
    exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
*/
 function exportexcel($data=array(),$title=array(),$filename='report'){
    header("Content-type:application/octet-stream");
    header("Accept-Ranges:bytes");
    header("Content-type:application/vnd.ms-excel");  
    header("Content-Disposition:attachment;filename=".$filename.".xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    //导出xls 开始
    if (!empty($title)){
        foreach ($title as $k => $v) {
            $title[$k]=iconv("UTF-8", "GB2312",$v);
        }
        $title= implode("\t", $title);
        echo "$title\n";
    }
    if (!empty($data)){
        foreach($data as $key=>$val){
            foreach ($val as $ck => $cv) {
                $data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
            }
            $data[$key]=implode("\t", $data[$key]);
            
        }
        echo implode("\n",$data);
    }
 }

function Debug($ArrayOrStr){

	echo "<pre>";

	if(is_array($ArrayOrStr)){
		print_r($ArrayOrStr);
	}
	else{
		echo $ArrayOrStr;
	}
	
	echo "<pre>";
	exit;
}

function Form_Select($value,$select=1)
{
	$value==$select?$check="selected":$check= $value;
	return $check;
}

function MultipleSelectArr($TableName, $SelectName, $Display, $Value = "", $Current = "", $disabled = false, $required = "", $title=""){

	$Model = new Model();
	$sqlB = new StringBuilder();

	$sqlB->Append("SELECT ");
	$sqlB->Append("   " . $Display .  " ");

	if($Value != ""){
		$sqlB->Append(" , " . $Value .  " ");
	}

	$sqlB->Append("FROM  " . $TableName . " ");
	$sqlB->Append(";");

	$TableList = $Model->query($sqlB->ToString());

	$html = "<div class='SelectField'>";
	if($disabled){
		$html .= "<select id='" . $SelectName . "' name='" . $SelectName . "' readonly = \"readonly\" required='" .  $required . "' title='" . $title . "' >";
	}
	else{
		$html .= "<select id='" . $SelectName . "' name='" . $SelectName . "' required='" .  $required . "' title='" . $title . "'>";
	}
	
	$html .= "<option value=\"\">请选择</option>";

	for($i = 0; $i < count($TableList); $i++){
		if($Value != ""){
			if($Current != "" && $TableList[$i][$Value] == $Current)
			{
				$html .= "<option value='" . $TableList[$i][$Value] . "' selected>" . $TableList[$i][$Display] . "</option>";
			}
			else{
				$html .= "<option value='" . $TableList[$i][$Value] . "'>" . $TableList[$i][$Display] . "</option>";
			}
			
		}
		else{

			if($Current != "" && $TableList[$i][$Display] == $Current)
			{
				$html .= "<option value='" . $TableList[$i][$Display] . "' selected>" . $TableList[$i][$Display] . "</option>";
			}
			else{
				$html .= "<option value='" . $TableList[$i][$Display] . "'>" . $TableList[$i][$Display] . "</option>";
			}

			
		}
	}

	$html .= "</select>";
	$html .= "</div>";
	echo $html;
}

function FData($value, $Type, $Length = 0){

	if($Type == "Text"){
		if($value == ""){
			return "Null";
		}
		else{
			return "'" . $value . "'";
		}
		
	}
}

function ReplaseUrl($URL){

	if($URL == "" || $URL == "Null"){
		return "";
	}
	$URLArr = explode("/",$URL);

	if(Count($URLArr) == 4){
		$URLArr[3] = "s_".$URLArr[3];
		return "/". $URLArr[1]."/". $URLArr[2]. "/".$URLArr[3];
	}
	else{
		return $URL;
	}
	
}
?>